/**
 * 非常规工作日窗口
 */
Ext.namespace("Ext.ux.workholiday");

Ext.ux.workholiday.SpecialDayDetailWindow = Ext.extend(Ext.Window, {

	width: 600,
	height: 280,
	layout: 'fit',
	plain : true,
	modal: true,
	border: false,
	
	detailPanel: null,
	closeAction: 'hide',
	id: 'specialTimeDetailWindow',
	
	isLoadState: false,
	lastStartTime: "08:30",
	lastEndTime: "17:30",
	initComponent:function(){
		Ext.ux.workholiday.SpecialDayDetailWindow.superclass.initComponent.call(this);
		
		// 编辑附件数据时的JSON解析
		var formReader = new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'totalCount',
			id : 'id'
		},[
			{name: 'id', mapping: 'id'}, 
			{name: 'day', mapping: 'day'}, 
			{name: 'isHoliday', mapping: 'isHoliday'},
			{name: 'isAllDay', mapping: 'isAllDay'},
			{name: 'startTime', mapping: 'startTime'},
			{name: 'endTime', mapping: 'endTime'},
			{name: 'memo', mapping: 'memo'}
		]);

	
		var inputPanel = new Ext.form.FormPanel({
    		labelAlign: 'right',
       		frame: true,
       		reader: formReader,
       		items: [{
       		 	layout: 'form',
       		 	labelWidth: 70,
       		 	xtype: 'fieldset',
		        title: '<span class="fs_tip">标志 <span class="fs_flag">*</span> 字段为必填项</span>',
	       		items:[{
					layout: 'column',
					items: [{
		       			columnWidth: .5,
		       			layout: 'column',
		       			items: [{
								columnWidth: .6,
								layout: 'form',
								items:[{
									checked: true,
									xtype: 'radio',
					                fieldLabel: '<span class="fs_flag">*</span>&nbsp;类型',
					                boxLabel: '假期',
					                name: 'isHoliday',
					                inputValue: 'Y'
								}]
								
							},{
								columnWidth: .4,
								layout: 'form',
								items:[{
									hideLabel : true,
					                xtype: 'radio',
					                boxLabel: '加班',
					                name: 'isHoliday',
					                inputValue: 'N'
								}]
							}]
		       		}]
				},{
	       			layout: 'column',
		       		items:[{
		       			columnWidth: .99,
		       			layout: 'column',
		       			items: [{
							xtype: 'hidden',
							name: 'id'
						},{
							columnWidth: .5,
							layout: 'form',
							items:[{
								fieldLabel: '<span class="fs_flag">*</span>&nbsp;日期',
				       			name: 'day',
				       			xtype: 'datefield',
				       			format: 'Y-m-d',
				       			allowBlank: false,
				       			anchor: '95%'
							}]
			       			
		       		 	},{
		       		 		columnWidth: .5,
							layout: 'form',
							items:[{
								xtype: 'radiogroup',
					            fieldLabel: '是否整天',
					            name: 'isAllDay',
					            items: [
					                {boxLabel: '是', name: 'isAllDay', inputValue: 'Y', checked: true},
					                {boxLabel: '否', name: 'isAllDay', inputValue: 'N'}
					            ],
					            listeners: {
					            	'change': function(radioGroup, checked){
					            		//如果选择'是'
					            		if(checked.inputValue == 'Y'){
					            			//保存上次设置的值
					            			this.saveLastTime();
					            			
					            			//清空开始时间和结束时间
					            			this.clearTimeField();
					            			
					            			//使设置开始时间和结束时间的组件不可用
											this.disableTimeField(true);
											
					            		}
					            		//如果选择'否'
					            		else{
					            			//如果是第一次加载form表单
					            			if(this.isLoadState){
					            				//如果没有设置时间值
					            				if(this.isSetTimeValue()){
						            				this.setDefaultTime();
					            				}
					            			}
					            			else{
					            				this.setLastTime(this.lastStartTime, this.lastEndTime);
					            			}

					            			//使设置开始时间和结束时间的组件可用
											this.disableTimeField(false);
					            		}
					            		this.isLoadState = false;
					            	},
					            	scope: this
					            }
							}]
		       		 	}]
		       		}]
				},{
	       			layout: 'column',
		       		items:[{
		       			columnWidth: .5,
		       			layout: 'form',
		       			items: [{
			       			fieldLabel: '起始时间',
			       			disabled: true,
			       			name: 'startTime',
			       			minValue: '0:00',
   							maxValue: '23:00',
						    increment: 30,
			       			xtype: 'timefield',
			       			format: 'G:i',
			       			anchor: '95%'
		       		 	}]
		       		},{
		       			columnWidth: .5,
		       			layout: 'form',
		       			items: [{
		       		 		fieldLabel: '结束时间',
		       		 		disabled: true,
		       		 		xtype: 'timefield',
			       		 	name: 'endTime',
			       		 	minValue: '0:00',
						    maxValue: '23:00',
						    format: 'G:i',
						    increment: 30,
		       		 		anchor: '95%'
		       			}]
		       		}]
				},{
		       		 	layout: 'column',
		       		 	items:[{
		       		 		columnWidth: 1,
		       		 		layout: 'form',
		       		 		items: [{
		       		 			fieldLabel: '备注',
			       		 		xtype: 'textarea',
			       		 		name: 'memo',
			       		 		anchor: '97%'
		       		 		}]
		       		 		
		       		 	}]
	       		 }]
       		 }],
       		 buttons: [{
				text: '&nbsp;&nbsp;保存',
				width: 90,
				iconCls: 'icon-middle-save',
				scale: 'medium',
				handler: this.save,
				scope: this
			},{
				text: '&nbsp;&nbsp;关闭窗口',
				width: 90,
				iconCls: 'icon-middle-cancel',
				scale: 'medium',
				handler: function(){
					this.hide();
				},
				scope: this
			}]
    	});
		
    	this.add(inputPanel); 
    	this.detailPanel = inputPanel;
	},
	
	//加载附件数据
	loadById: function(id){
		this.isLoadState = true;
		this.detailPanel.form.load({
			url: 'wh-special-time!input.action?id=' + id,
			waitMsg: '正在载入数据...',
			failure: function() {
				Ext.Msg.alert('系统提示', '无法加载指定非常规工作日信息！');
			}
		});
	},
	clearForm: function(){
		this.detailPanel.form.reset();
	},
	setDefaultDay: function(){
		this.detailPanel.form.findField('day').setValue(new Date());
	},
	setDefaultTime: function(){
		this.detailPanel.form.findField('startTime').setValue("08:30");
		this.detailPanel.form.findField('endTime').setValue("17:30");
	},
	saveLastTime: function(){
		this.lastStartTime = this.detailPanel.form.findField('startTime').getValue();
		this.lastEndTime = this.detailPanel.form.findField('endTime').getValue();
	},
	setLastTime:function(lastStartTime, lastEndTime){
		this.detailPanel.form.findField('startTime').setValue(lastStartTime);
		this.detailPanel.form.findField('endTime').setValue(lastEndTime)
	},
	clearTimeField: function(){
		this.detailPanel.form.findField('startTime').setValue("");
		this.detailPanel.form.findField('endTime').setValue("");
	},
	disableTimeField: function(disabled){
		this.detailPanel.form.findField('startTime').setDisabled(disabled);
		this.detailPanel.form.findField('endTime').setDisabled(disabled);
	},
	isSetTimeValue: function(){
		return this.detailPanel.form.findField('startTime').getValue() == "" && this.detailPanel.form.findField('endTime').getValue() == ""
	},
	//保存
	save: function(){
		var startTime = Date.parseDate(this.detailPanel.form.findField('startTime').getValue(), 'G:i');
		var endTime = Date.parseDate(this.detailPanel.form.findField('endTime').getValue(), 'G:i');
		
		var startHour = (startTime != null?startTime.getHours():null);
		var startMinute = (startTime != null?startTime.getMinutes():null);
		var endHour = (startTime != null?endTime.getHours():null);
		var endMinute = (startTime != null?endTime.getMinutes():null);
		
		if(this.detailPanel.form.isValid()){
			this.detailPanel.form.submit({
				url: 'wh-special-time!save.action',
				success: function(form, action){
					Ext.Msg.alert('系统提示', '保存非常规工作日信息成功');
					this.hide();
					Ext.getCmp('specialTimeGrid').getStore().reload();
				},
				failure: function(){
					Ext.Msg.alert('系统提示', '保存非常规工作日信息失败！');
				},
				params: {
					startHour: startHour,
					startMinute: startMinute,
					endHour: endHour,
					endMinute: endMinute
				},
				scope: this
			});
		}
		
		
	}

});